let express = require('express');
let router = express.Router();
let multer =require('multer')
let fs = require('fs-extra');
let path = require('path');
let moment = require('moment');
let upload = multer({
    dest: 'upload/',
  });
//图片上传接口
// 上传图片
router.post('/', upload.single('file'),  async function(req, res) {
    let  file =req.file;
    let  dirName='avatar';
 let type = file.mimetype.split('/')[1];
 let fileName = `${file.filename}.${type}`; // 文件重命名
 let date = new Date();
 let today = moment(date).format('YYYYMMDD');
 let filepath = path.join(__dirname + '/../../../upload/'+dirName+'/');
 //拼接文件名称
 let newFilePath = path.join(filepath, fileName);
 //上传文件
 fs.renameSync(file.path, newFilePath);
 let imgUrl ='/'+dirName+'/'+fileName;

 res.json({status:200,msg:'success',imgUrl :imgUrl});
});

//apk 文件上传接口
router.post('/apk',upload.single('file'),async function(req,res){
  let file  = req.file;
  let  dirName = 'apk';
  let type = 'apk';
  let fileName = `${file.filename}.${type}`; // 文件重命名
  let filepath = path.join(__dirname + '/../../../upload/'+dirName+'/');
  //拼接文件名称
  let newFilePath = path.join(filepath, fileName);
  //上传文件
  fs.renameSync(file.path, newFilePath);
  let imgUrl ='/'+dirName+'/'+fileName;
 
  res.json({status:200,msg:'success',imgUrl :imgUrl});
})

module.exports=router;